﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;

namespace Web.Cms.Roles
{
    public partial class RoleManagement : AdminUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtRoleName.Focus();
            }
        }

        protected void grvRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int _roleId = ConvertUtility.ToInt32(e.CommandArgument);

            ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
            switch (e.CommandName)
            {
                case "deleterole":
                    try
                    {
                        RoleInfo _roleInfo = RoleController.GetRole(_roleId);
                        RoleController.DeleteRoleInRolePermision(_roleId);
                        RoleController.DeleteUserInRoleByRoleID(_roleId);
                        RoleController.DeleteRole(_roleId);
                        string title = _roleInfo.RoleName;
                        // Insert Cores_Audits
                        AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Delete, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                                   _roleId, title, 1,
                                                   DateTime.Now);
                        ClearData();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    break;
                case "rolepermision":
                    Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "rolepermision", new string[] { "tabkey", "roleid" }, new string[] { "admin_role", _roleId.ToString() }));
                    break;
                case "usersinrole":
                    RoleInfo _roleInfos = RoleController.GetRole(_roleId);
                    // Insert Cores_Audits
                    AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.View, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                                _roleId, "xem thành viên trong nhóm: " + _roleInfos.RoleName, 1,
                                                 DateTime.Now);
                    Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "admin_usersinrole", new string[] { "tabkey", "usersinroleId" }, new string[] { "admin_role", _roleId.ToString() }));
                    break;
                case "Userzones":
                    RoleInfo _roleZone = RoleController.GetRole(_roleId);
                    // Insert Cores_Audits
                    AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.View, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                                   _roleId, "Phân quền chuyên mục: " + _roleZone.RoleName, 1,
                                                    DateTime.Now);
                    Response.Redirect(
                      UIProcess.GetCPPage(CurrentPortalID, "UserRoleZones", new string[] { "tabkey", "userinroleid" },
                                          new string[] { "UserRoleZones", e.CommandArgument.ToString() }));
                    break;

                case "UserCity":
                    RoleInfo _roleCity = RoleController.GetRole(_roleId);
                    // Insert Cores_Audits
                    AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.View, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                                   _roleId, "Phân quền tỉnh thành: " + _roleCity.RoleName, 1,
                                                    DateTime.Now);

                    //End
                    
                    Response.Redirect(
                     UIProcess.GetCPPage(CurrentPortalID, "UserRoleCity", new string[] { "tabkey", "userinroleid" },
                                         new string[] { "UserRoleCity", e.CommandArgument.ToString() }));

                    break;
                case "rolefunction":
                    {
                        RoleInfo _roleInfo = RoleController.GetRole(_roleId);
                        // Insert Cores_Audits
                        AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.View, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                                   _roleId, "xem phân quyền chức năng nhóm: " + _roleInfo.RoleName, 1,
                                                    DateTime.Now);
                        //End
                        Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "rolepermision", new string[] { "tabkey", "roleid" }, new string[] { "admin_role", _roleId.ToString() }));
                        break;
                    }
                default:
                    break;
            }
        }

        protected void grvRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int _roleId = ConvertUtility.ToInt32(grvRoles.SelectedValue);
                RoleInfo _role = RoleController.GetRole(_roleId);
                txtRoleID.Text = _role.RoleID.ToString();
                txtRoleName.Text = _role.RoleName;
                txtDescription.Text = _role.Description;
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.View, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                            ConvertUtility.ToInt32(txtRoleID.Text), txtRoleName.Text, 1,
                                            DateTime.Now);
                //End    
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                RoleInfo _role = RoleController.GetRole(ConvertUtility.ToInt32(txtRoleID.Text));
                _role.RoleID = ConvertUtility.ToInt32(txtRoleID.Text);
                _role.RoleName = txtRoleName.Text;
                _role.Description = txtDescription.Text;
                RoleController.UpdateRole(_role);
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                            ConvertUtility.ToInt32(txtRoleID.Text), _role.RoleName, 1,
                                            DateTime.Now);
                ClearData();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                RoleInfo _role = new RoleInfo();
                _role.PortalID = CurrentPortalID;
                _role.RoleName = txtRoleName.Text;
                _role.Description = txtDescription.Text;
                txtRoleID.Text = RoleController.CreateRole(_role).ToString();
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Create, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                            ConvertUtility.ToInt32(txtRoleID.Text), _role.RoleName, 1,
                                            DateTime.Now);
                ClearData();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnDeleteRole_Click(object sender, EventArgs e)
        {
            try
            {
                int roleId = ConvertUtility.ToInt32(txtRoleID.Text);
                RoleInfo _roleInfo = RoleController.GetRole(roleId);
                RoleController.DeleteRoleInRolePermision(roleId);
                RoleController.DeleteUserInRoleByRoleID(roleId);
                RoleController.DeleteRole(ConvertUtility.ToInt32(txtRoleID.Text));

                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                            ConvertUtility.ToInt32(txtRoleID.Text), _roleInfo.RoleName, 1,
                                            DateTime.Now);
                ClearData();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        public void ClearData()
        {
            txtRoleID.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtRoleName.Text = string.Empty;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            grvRoles.DataSource = RoleController.GetRoles(CurrentPortalID);
            grvRoles.RowDataBound += new GridViewRowEventHandler(grvRoles_RowDataBound);
            grvRoles.DataBind();
            if (txtRoleID.Text == string.Empty)
            {
                btnAdd.Visible = true;
                btnUpdate.Visible = false;
                btnDeleteRole.Visible = false;
                divInsertItem.Visible = false;
            }
            else
            {
                btnAdd.Visible = false;
                btnUpdate.Visible = true;
                btnDeleteRole.Visible = true;
                divInsertItem.Visible = true;
            }
            RoleInfo _rolesInfo = RoleController.GetRole(ConvertUtility.ToInt32(txtRoleID.Text));
            if (_rolesInfo != null)
            {
                btnDeleteRole.Attributes.Add("onclick", MiscUtility.DeleteConfirm("nhóm quyền", _rolesInfo.RoleName));
            }

            RolePermisionInfo _rolePermesionInfo =
            RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
            if (_rolePermesionInfo == null) return;
            if (_rolePermesionInfo.Write == 0)
            {
                btnAdd.Visible = false;
                btnDeleteRole.Visible = false;
                btnUpdate.Visible = false;
                divInsertItem.Visible = false;
            }
        }
        protected void grvRoles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RoleInfo _rolesInfo = (RoleInfo)e.Row.DataItem;
                Label lblSTT = (Label)e.Row.FindControl("lblSTT");
                lblSTT.Text = Convert.ToString(e.Row.DataItemIndex + 1);
                //Button btnRoleFunction = (Button) e.Row.FindControl("btnRoleFunction");
                Button btnDelete = (Button)e.Row.FindControl("btnDelete");
                btnDelete.Attributes.Add("onclick", MiscUtility.DeleteConfirm("nhóm quyền", _rolesInfo.RoleName));
                //List<UserInRoleInfo> lstUsersInRole = RoleController.GetUsersInRole(_rolesInfo.RoleID);
                //if(lstUsersInRole.Count>0)
                //{
                //    btnDelete.Enabled = false;
                //    btnDelete.CssClass = "Icon_DeleteOff";
                //}

                RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
                if (_roleInfo == null) return;
                if (_roleInfo.Write == 0)
                {
                    btnDelete.Enabled = false;
                    btnDelete.CssClass = "Icon_DeleteOff";
                }
            }
        }

        protected void grvRoles_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvRoles.PageIndex = e.NewPageIndex;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.DEFAULT_CP_PAGE);
        }
        protected void lnkInsertItem_Click(object sender, EventArgs e)
        {
            ClearData();
        }
    }
}